﻿package com.xuanque.mahjongDice.util
{
        import flash.display.DisplayObject;
        import flash.display.Graphics;
        import flash.display.Sprite;
        import flash.display.Stage;
        import flash.events.MouseEvent;
        import flash.filters.DropShadowFilter;
        import flash.text.TextField;
        import flash.text.TextFieldAutoSize;
        import flash.text.TextFormat;

        public class ToolTip extends Sprite
        {
                 private var m_stage:Stage;//注，tooltip必须加到stage下 
                 private var m_ntxtcolor:uint=0x000000;
                 private var m_ntxtsize:int=12;
                 private var m_nbordercolor:uint=0x000000;
                 private var m_nbgcolor:uint=0xFFFFCC;
                 private var m_nmaxtxtwidth:Number=200;
                 private static var Instance:ToolTip;
                
                private var m_arrDOTips:Array = new Array();
                private var m_tipTxt:TextField;
                public function ToolTip()
                {
                                
                }
                //获取全局唯一实例
                public static function getInstance():ToolTip
                {
                	    if(!Instance){
                	        Instance = new ToolTip();
                	    }
                        return Instance;
                }
                
				public function setStaticObjectNull():void{
					Instance = null;
				}
				
				
                public function init():void{
                                Instance.visible = false;
                                Instance.m_tipTxt = new TextField();
                                Instance.m_tipTxt.autoSize = TextFieldAutoSize.LEFT;
                                Instance.m_tipTxt.selectable = false;
                                Instance.addChild(Instance.m_tipTxt);
                                m_stage.addChild(Instance);
                }
                //清空tooltips，注：不是隐藏所有，而是彻底清空，如果要隐藏，某一时刻又显示出来的话，采用hide和show
                public  function removeToolTip():void
                {
                        for(var i:int=0; i<getInstance().DOTips.length; i++)
                        {
                                getInstance().DOTips[i].DO.removeEventListener(MouseEvent.ROLL_OVER, showtip);
                                getInstance().DOTips[i].DO.removeEventListener(MouseEvent.ROLL_OUT, hidetip);
                                getInstance().DOTips[i].DO.removeEventListener(MouseEvent.MOUSE_MOVE, movetip);  
                                if(getInstance().DOTips[i].parent!=null){
								     getInstance().DOTips[i].parent.removeChild(getInstance().DOTips[i]);
								}
                                getInstance().DOTips[i].D0 = null; 
                                getInstance().DOTips[i] = null;                           
                         }
                                if(getInstance().parent!=null){
                                  getInstance().parent.removeChild(getInstance());
                                }
                                //getInstance() = null;
                                Instance = null;
                               getInstance().DOTips.splice();
                }
                //暂时隐藏
                public  function hideToolTip():void
                {
                        m_stage.removeChild(getInstance());
                }
                //再次show
                public  function showToolTip():void
                {
                        m_stage.addChild(getInstance());
                }
                //添加某DO的tip绑定
                public  function BindDO(DO:DisplayObject,info:String):void
                {
                        //test if already been binded
                        var myFilter:DropShadowFilter = new DropShadowFilter(0,0,0x00c0ff,0.75,2,2,2,2,false,false,false);
                        if(TestDOBinding(DO) == -1)
                        {
                                //add to array
                                var dotip:Object = {DO:DO, info:info};
                                getInstance().DOTips.push(dotip);
                                DO.addEventListener(MouseEvent.ROLL_OVER, showtip);
                                DO.addEventListener(MouseEvent.ROLL_OUT, hidetip);
                                DO.addEventListener(MouseEvent.MOUSE_MOVE, movetip);
                                //mc投影效果
                                //DO.filters = new Array(myFilter);

                        }
                }
                //更改某绑定DO的文字信息
                public  function setDOInfo(DO:DisplayObject, info:String):void
                {
                        if(TestDOBinding(DO) == -1)
                                BindDO(DO, info);
                        else
                                getInstance().DOTips[TestDOBinding(DO)].info = info;
                }
                //测试是否已经绑定，绑定则返回数组中的次序，否则返回-1
                public  function TestDOBinding(DO:DisplayObject):int
                {
                        var flag:Boolean = false;
                        for(var i:int=0; i<getInstance().DOTips.length; i++)
                        {
                                if(getInstance().DOTips[i].DO == DO)
                                {
                                        flag = true;
                                        break;
                                }
                        }
                        return (flag ? i : -1);
                }
                private  function getDOBindingObj(DO:DisplayObject):Object
                {
                        
                        for(var i:int=0; i<getInstance().DOTips.length; i++)
                        {
                                if(getInstance().DOTips[i].DO == DO)
                                {
                                        return getInstance().DOTips[i];
                                }
                        }
                        return null;
                }
                
                public  function updateInfo(obj:DisplayObject,info:String):void{
                	var ob:Object = getDOBindingObj(obj);
                   if(ob){
                      ob.info = info;
                   }
                }
                //
                private  function showtip(evt:MouseEvent):void
                {
                        getInstance().x = evt.stageX;
                        getInstance().y = evt.stageY + 20;//注，20是鼠标高度
                        getInstance().m_tipTxt.wordWrap = false;
                        getInstance().m_tipTxt.text = getInstance().DOTips[TestDOBinding(evt.target as DisplayObject)].info;
                        updatetip();
                        getInstance().visible = true;
                }
                private  function hidetip(evt:MouseEvent):void
                {
                        getInstance().visible = false;
                }
                private  function movetip(evt:MouseEvent):void
                {
                        getInstance().x = evt.stageX;
                        getInstance().y = evt.stageY + 20;
                }
                private  function updatetip():void
                {
                        getInstance().m_tipTxt.textColor = m_ntxtcolor;
                        if(getInstance().m_tipTxt.width > m_nmaxtxtwidth)
                        {
                                getInstance().m_tipTxt.wordWrap = true;
                                 getInstance().m_tipTxt.width = m_nmaxtxtwidth;
                        }
                        var tf:TextFormat = new TextFormat();
                        tf.size = m_ntxtsize;
                        getInstance().m_tipTxt.setTextFormat(tf);
                        //
                        var gp:Graphics = getInstance().graphics;
                        gp.clear();
                        gp.lineStyle(0, m_nbordercolor);
                        gp.beginFill(m_nbgcolor);
                        gp.drawRect(0, 0, getInstance().m_tipTxt.width, getInstance().m_tipTxt.height);
                        gp.endFill();
                        //
                }
                //
                public  function setStage(stage:Stage):void
                {
                        m_stage = stage;
                        init();
                }
                
                public  function getStage():Stage
                {
                        return m_stage;
                }
                public  function setTipProperty(txtcolor:uint=0x000000, txtsize:int=12, maxtxtwidth:int=200, bordercolor:uint=0x000000, bgcolor:uint=0xFFFFCC):void
                {
                        m_ntxtcolor = txtcolor;
                        m_ntxtsize = txtsize;
                        m_nmaxtxtwidth = maxtxtwidth;
                        m_nbordercolor = bordercolor;
                        m_nbgcolor = bgcolor;
                }
                //
                private function get DOTips():Array
                {
                        return m_arrDOTips;
                }
        }
}